from selenium.webdriver.common.by import  By
from pages.base_page import BasePage
from config.config import HOST

'''财务页面'''


class FinancePage(BasePage):
    # 添加应收款页面url
    url='http://{}/crm/index.php?m=finance'.format(HOST)
    # 添加应收款定位
    add_credit_locator = (
    By.CSS_SELECTOR, 'body > div.container > div.row > div:nth-child(1) > div.pull-right > a:nth-child(1)')
    # 定位第一条应收款
    click_first_credit_locator=(By.CSS_SELECTOR,'#form1 > table > tbody > tr:nth-child(1) > td:nth-child(1) > input')
    # 定位最后一条应收款
    click_last_credit_locator = (By.CSS_SELECTOR, '#form1 > table > tbody > tr:nth-child(14) > td:nth-child(1) > input')
    # 定位最后一条应收款的查看
    look_last_btn_locator = (By.CSS_SELECTOR, '#form1 > table > tbody > tr:nth-child(10) > td:nth-child(11) > a:nth-child(1)')
    # 定位最后一条应收款的编辑按钮
    edit_last_btn_locator = (By.CSS_SELECTOR, '#form1 > table > tbody > tr:nth-child(14) > td:nth-child(11) > a:nth-child(2)')
    # 定位回收站
    recycle_locator = (By.XPATH, '/html/body/div[5]/p/a[14]')
    # 定位搜索框
    search_input_locator = (By.XPATH, '//*[@id="search"]')
    #  定位搜索按钮
    search_btn_locator = (By.XPATH, '//*[@id="searchForm"]/ul/li[4]/button')
    # 定位搜索列表
    search_result_locator=(By.XPATH,'/html/body/div[5]/div[2]/div[2]/form/table/tbody/tr[1]/td[2]/a')
    # 定位每页显示下拉框
    pages_each_display_locator = (By.ID, 'listrows')
    # 定位应收款页面的删除按钮
    del_credit_locator=(By.ID,'delete')
    # 定位删除成功提示语
    del_success_tips=(By.CSS_SELECTOR,'body > div.container > div.alert.alert-success')
    # 定位客户名为空的提示语
    customer_isnull_tips_locator= (By.CSS_SELECTOR, 'body > div.container > div.alert.alert-error')


    def click_add_credit(self):
        '''点击添加应收款'''
        self.driver.find_element(*self.add_credit_locator).click()

    def select_first_credit(self):
        '''点击第一条应收款'''
        self.driver.find_element(*self.click_first_credit_locator).click()

    def click_last_credit_checkbox(self):
        '''点击最后一条应收款'''
        self.driver.find_element(*self.click_last_credit_locator).click()

    def click_last_look(self):
        '''点击最后一条应收款的查看'''
        self.driver.find_element(*self.look_last_btn_locator).click()

    def click_last_edit(self):
        '''点击修改按钮'''
        self.driver.find_element(*self.edit_last_btn_locator).click()

    def click_recycle(self):
        '''点击回收站'''
        self.driver.find_element(*self.recycle_locator).click()

    def input_exist_search(self,credit):
        '''在搜索框输入存在的应收款'''
        self.driver.find_element(*self.search_input_locator).send_keys(credit)

    def click_search(self):
        '''点击搜索按钮'''
        self.driver.find_element(*self.search_btn_locator).click()

    def get_search_results(self):
        '''获取搜素结果'''
        a_tags = self.driver.find_elements(*self.search_result_locator)
        if len(a_tags) > 0:
            return '存在'
        else:
            return '不存在'

    def click_pages_each_display(self):
        '''点击每页显示下拉框'''
        dropdown=self.driver.find_element(*self.pages_each_display_locator)
        return dropdown

    def click_del_credit(self):
        '''删除第一条应收款'''
        self.driver.find_element(*self.del_credit_locator).click()

    def get_credit_info_url(self):
        '''获取应收款详情页的url'''
        return self.current_url()

    def get_credit_edit_url(self):
        ''' 获取修改应收款页面的url'''
        return self.current_url()

    def get_recycle_url(self):
        ''' 获取回收站页面的url'''
        return self.current_url()

    def get_del_success_tips(self):
        '''获取断言成功的提示语'''
        tips = self.driver.find_element(*self.del_success_tips).text
        return tips

    def customer_isnull_tips(self):
        '''获取客户名为空的提示语'''
        isnull_tips=self.driver.find_element(*self.customer_isnull_tips_locator).text
        return isnull_tips



